//
//  SEGActivityCollectMyUploadViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/4.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityCollectMyUploadViewController.h"
#import "SEGActivityMyUploadCell.h"
#import "SEGActivityMyUploadModel.h"
#import "SEGActivityDetailToolBarView.h"
#import <Commom/YYPhotoGroupView.h>
#import "SEGActivityCollectMyUploadViewModel.h"

@interface SEGActivityCollectMyUploadViewController () <UITableViewDelegate, UITableViewDataSource,SEGActivityDetailToolBarDelegate,SEGActivityMyUploadCellDelegate>



@property (nonatomic, strong) UIView* noDataView;

/**
 主视图
 */
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong) SEGActivityCollectMyUploadViewModel *viewModel;
@end

@implementation SEGActivityCollectMyUploadViewController

- (instancetype)initWithActId:(NSString *)actId {
    if (self = [super init]) {
        self.viewModel = [[SEGActivityCollectMyUploadViewModel alloc] initWithActId:actId];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我发布的";
    self.view.backgroundColor = kColor_bg;
    [self showCustomNavigationBackButton];
    
    [self.view addSubview:self.tableView];
    
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [weakSelf headerRereshing];
    } footerRefreshBlock:^{
        [weakSelf footerRereshing];
    }];
    [self.tableView beginHeaderRefreshing];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

#pragma mark - 刷新控件
//头部刷新逻辑
- (void)headerRereshing {
    [self loadMyCommentsWithRefresh:YES];
}

//底部刷新逻辑
- (void)footerRereshing {
    [self loadMyCommentsWithRefresh:NO];
}

#pragma mark - 数据请求
- (void)loadMyCommentsWithRefresh:(BOOL)refresh {
    
    [[self.viewModel.listCommand execute:[NSNumber numberWithBool:refresh]] subscribeNext:^(id  _Nullable x) {
        [self.tableView stopAnimatingWindmill];
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    } completed:^{
        if (self.viewModel.activityManager.myCommentsList.count == 0) {
            self.noDataView.hidden = NO;
            self.tableView.hidden = YES;
        }else {
            self.noDataView.hidden = YES;
            self.tableView.hidden = NO;
        }
        [self.tableView reloadData];
    }];
}

#pragma mark - SEGActivityMyUploadCellDelegate
- (void)myUploadCellImageTapWithImageUrlArray:(NSArray *)arr SelectedImageView:(UIImageView*)image SelectedIndex:(NSInteger)index
{
    [SEGPhotoBrower segBrowPhotoWithDataArray:arr
                            SelectedImageView:image
                                SelectedIndex:index
                                     FromView:self.navigationController.view];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.activityManager.myCommentsList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SEGActivityMyUploadCell *cell = [tableView dequeueReusableCellWithIdentifier:kActivityMyUploadCellIdentifier forIndexPath:indexPath];
    [cell setUpCellWithMyUploadModel:self.viewModel.activityManager.myCommentsList[indexPath.section]];
    cell.delegate = self;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return 44;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.1;
    } else {
        return 10;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 私有方法



#pragma mark - getter
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-kNavigationBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[SEGActivityMyUploadCell class] forCellReuseIdentifier:kActivityMyUploadCellIdentifier];
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (UIView *)noDataView
{
    if (!_noDataView) {
        _noDataView = [[UIView alloc] init];
        _noDataView.backgroundColor = kColor_W;
        [self.view addSubview:_noDataView];
        [_noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        
        UIImageView *imageView;
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_page")]];;
        [_noDataView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noDataView);
            make.top.equalTo(_noDataView).offset(130);
        }];
        
        UILabel *lable = [[UILabel alloc] init];
        lable.font = kFont_13;
        lable.textColor = kColor_3;
        lable.textAlignment = NSTextAlignmentCenter;
        lable.numberOfLines = 2;
        lable.text = @"暂时还没有任何内容";

        [_noDataView addSubview:lable];
        [lable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noDataView);
            make.top.equalTo(imageView.mas_bottom).offset(20);
        }];
    }
    return _noDataView;
}



@end

